package com.learn.core.controller.manager;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.learn.common.annotation.Log;
import com.learn.common.annotation.UserLoginToken;
import com.learn.common.enums.BusinessType;
import com.learn.common.enums.OperatorType;
import com.learn.common.enums.ResultEnum;
import com.learn.common.utils.JwtUtil;
import com.learn.common.utils.PageResult;
import com.learn.common.utils.ResultUtil;
import com.learn.common.utils.StringUtils;
import com.learn.common.utils.result.CommonResult;
import com.learn.core.domain.LTeacher;
import com.learn.core.domain.LTheme;
import com.learn.core.dto.LTeacherDTO;
import com.learn.core.service.ILTeacherService;
import com.learn.framework.web.controller.WebController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @auther CodeGenerator
 * @create 2021-03-13 19:35:35
 * @describe 教员表前端控制器
 */

@Api("教员")
@RestController
@RequestMapping("/manager/teacher")
public class LTeacherController  extends WebController<LTeacher> {
    @Autowired
    private ILTeacherService iLTeacherService;

    //列表
    @ApiOperation(value = "获取列表")
    @UserLoginToken
    @PostMapping("/list")
    public PageResult<LTeacher> list(@RequestBody LTeacherDTO lTeacherDTO){
        try {
            if(lTeacherDTO==null||lTeacherDTO.getPage()==null||lTeacherDTO.getRows()==null){
                return new PageResult<LTeacher>(null, null, ResultEnum.ERROR);
            }
            PageHelper.startPage(lTeacherDTO.getPage(),lTeacherDTO.getRows());
            QueryWrapper<LTeacher> queryWrapper = new QueryWrapper<LTeacher>();
            if(StringUtils.isNotEmpty(lTeacherDTO.getTeacherTitle()))
                queryWrapper.eq("teacher_title",lTeacherDTO.getTeacherTitle());
            if(StringUtils.isNotEmpty(lTeacherDTO.getTeacherName()))
                queryWrapper.eq("teacher_name",lTeacherDTO.getTeacherName());
            if(StringUtils.isNotEmpty(lTeacherDTO.getStutes()))
                queryWrapper.eq("stutes",lTeacherDTO.getStutes());
            if(lTeacherDTO.getSortType()!=null&&lTeacherDTO.getSortBy()!=null)
                queryWrapper.orderBy(true,lTeacherDTO.getSortType(),lTeacherDTO.getSortBy());
            Page<LTeacher> lTeachers = (Page<LTeacher>) iLTeacherService.list(queryWrapper);
            if (CollectionUtils.isEmpty(lTeachers)) {
                return new PageResult<>(null, null, ResultEnum.NOT_EEIST);
            }
            // 返回
            return new PageResult(lTeachers.getTotal(), lTeachers, ResultEnum.SUCCESS);
        }catch (Exception e){
            return new PageResult(null,null,ResultEnum.ERROR);
        }
    }


    //新增
    @ApiOperation(value = "添加")
    @PostMapping("/add")
    @UserLoginToken
    @Log(title = "添加主讲人", businessType = BusinessType.INSERT, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> add(@RequestBody LTeacher lTeacher, HttpServletRequest request){
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        try {
            if(lTeacher!=null) {
                if (StringUtils.isEmpty(lTeacher.getTeacherName())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_NAME);
                }
                if (StringUtils.isEmpty(lTeacher.getStutes())) {
                    lTeacher.setStutes("1");
                }
                if (StringUtils.isEmpty(lTeacher.getTeacherType())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_TYPE);
                }
                if (StringUtils.isEmpty(lTeacher.getTeacherMobile())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_MOBILE);
                }
                if (StringUtils.isEmpty(lTeacher.getTeacherAddress())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_ADDRESS);
                }
                if (StringUtils.isEmpty(lTeacher.getTeacherMajor())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_MAJOR);
                }
                if (StringUtils.isEmpty(lTeacher.getTeacherSex())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_SEX);
                }
                if (StringUtils.isEmpty(lTeacher.getDescription())) {
                    return (CommonResult) ResultUtil.error(ResultEnum.NOT_TEACHER_DESCRIPTION);
                }
                lTeacher.setCreateUser(userId);
                lTeacher.setCreateTime(new Date());
                if (iLTeacherService.save(lTeacher)) {
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e){
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return  (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }


    //删除
    @ApiOperation(value = "删除")
    @PostMapping("/del")
    @UserLoginToken
    @Log(title = "删除主讲人", businessType = BusinessType.DELETE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> del(@RequestBody LTeacher lTeacher){
        try {
            if(lTeacher!=null){
                if(lTeacher.getTeacherId()!=null) {
                    if(iLTeacherService.removeById(lTeacher.getTeacherId()))
                    return (CommonResult) ResultUtil.success(ResultEnum.SUCCESS);
                }
            }
        }catch (Exception e)
        {
            return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }

    //更新
    @ApiOperation(value = "更新")
    @PostMapping("/edit")
    @UserLoginToken
    @Log(title = "更新主讲人", businessType = BusinessType.UPDATE, operatorType = OperatorType.SYSTEM)
    public CommonResult<Object> edit(@RequestBody LTeacher lTeacher, HttpServletRequest request) {
        String  token = request.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        if(lTeacher!=null){
            lTeacher.setUpdateTime(new Date());
            lTeacher.setUpdateUser(userId);
            if(iLTeacherService.updateById(lTeacher)){
                return (CommonResult)ResultUtil.success(ResultEnum.SUCCESS);
            }
        }
        return (CommonResult)ResultUtil.error(ResultEnum.ERROR);
    }
}

